#include "Menu.h"

//@author A0106829Y
void Menu::inputMenu(int nextLayer){
	if(_menuList.size() !=0  && nextLayer<0)
		backHome();
	else
		_menuList.push_back(nextLayer);

	return;
}

//@author U095387X
e_category Menu::getGlobalCategory(){
	return _mCategory;
}

//@author U095387X
e_method Menu::getGlobalMethod(){
	return _mMethod;
}

//@author U095387X
e_dateRange Menu::getGlobalDateRange(){
	return _mDateRange;
}

//@author A0106829Y
void Menu::backHome(){
	_menuList.resize(0);
}

//@author A0106829Y
void Menu::previousPage(){
	_menuList.pop_back();
}

//@author A0106829Y
string Menu::menuString(){
	ostringstream tempOutput;
	string output = "";

	if(_menuList.size() == 0){
		tempOutput << "MAIN MENU: SELECT AN ACTION\n";
		tempOutput << "1.\tRecord an expense\n";
		tempOutput << "2.\tRecord an income\n";
		tempOutput << "3.\tRecord a loan\n";
		tempOutput << "4.\tRecord a wish\n";
		tempOutput << "5.\tRead expenses\n";
		tempOutput << "6.\tRead incomes\n";
		tempOutput << "7.\tRead loans\n";
		tempOutput << "8.\tRead wishs\n";
		tempOutput << "9.\tUndo previous action\n";
		tempOutput << "10.\tShow Report\n";
		tempOutput << "11.\tImport Report\n";
		tempOutput << "12.\tEdit profile\n";
		tempOutput << "x.\tExit";
	}
	
	else{

		if(_menuList[0] == 1){
			if(_menuList.size() == 1){
				tempOutput << "Record a new expense: Choose a category\n";
				tempOutput << "1.\tFood\n";
				tempOutput << "2.\tTransportation\n";
				tempOutput << "3.\tEntertainment\n";
				tempOutput << "4.\tOthers\n\n";
				tempOutput << "x.\tBack to Main Menu";
			}else if(_menuList.size() == 2){
				switch (_menuList[1])
				{
				case 1:
					_mCategory = Food;
					break;
				case 2:
					_mCategory = Transportation;
					break;
				case 3:
					_mCategory = Entertainment;
					break;
				default:
					_mCategory = Others;
					break;
				}
				
				tempOutput << "Record a new expense: Choose a payment method\n";
				tempOutput << "1.\tCash\n";
				tempOutput << "2.\tCredit Card\n";
				tempOutput << "3.\tDebit Card\n";
				tempOutput << "x. Back to Main Menu";
			}else if(_menuList.size() == 3){
				switch (_menuList[2])
				{
				case 2:
					_mMethod = CreditCard;
					break;
				case 3:
					_mMethod = DebitCard;
					break;
				default:
					_mMethod = Cash;
					break;
				}
				tempOutput << "Record a new expense\n";
			}
			else{
				previousPage();
				output = menuString();
			}
		}

		else if(_menuList[0] == 2){
			if(_menuList.size() == 1)
				tempOutput << "Record a new income\n";
			else{
				previousPage();
				output = menuString();
			}
		}

		else if(_menuList[0] == 3){
			if(_menuList.size() == 1)
				tempOutput << "Record a new loan\n";
			else{
				previousPage();
				output = menuString();
			}
		}

		else if(_menuList[0] == 4){
			if(_menuList.size() == 1)
				tempOutput << "Record a new wish\n";
			else{
				previousPage();
				output = menuString();
			}
		}

		else if(_menuList[0] == 5){
			if(_menuList.size() == 1){
				tempOutput << "Choose a retrieve method for incomes:" << endl;
				tempOutput << "1.\tDaily" << endl;
				tempOutput << "2.\tMonthly" << endl;
				tempOutput << "3.\tUser defined Range" << endl << endl;
				tempOutput << "x. Back to Main Menu";
			}else if(_menuList.size() == 2){
				switch (_menuList[1])
				{
				case 1:
					_mDateRange = daily;
					break;
				case 2:
					_mDateRange = monthly;
					break;
				default:
					_mDateRange = dDateRange;
					break;
				}
				tempOutput << "Read expense\n";
			}
			else{
				previousPage();
				output = menuString();
			}
		}

		else if(_menuList[0] == 6){
			if(_menuList.size() == 1){
				tempOutput << "Choose a retrieve method for incomes:" << endl;
				tempOutput << "1.\tDaily" << endl;
				tempOutput << "2.\tMonthly" << endl;
				tempOutput << "3.\tUser defined Range" << endl << endl;
				tempOutput << "x. Back to Main Menu";
			}
			else if(_menuList.size() == 2){
				switch (_menuList[1])
				{
				case 1:
					_mDateRange = daily;
					break;
				case 2:
					_mDateRange = monthly;
					break;
				default:
					_mDateRange = dDateRange;
					break;
				}
				tempOutput << "Read incomes\n";
			}
			else{
				previousPage();
				output = menuString();
			}
		}

		else if(_menuList[0] == 7){
			if(_menuList.size() == 1){
				tempOutput << "Choose a retrieve method for loans:" << endl;
				tempOutput << "1.\tDaily" << endl;
				tempOutput << "2.\tMonthly" << endl;
				tempOutput << "3.\tUser defined Range" << endl << endl;
				tempOutput << "x. Back to Main Menu";
			}
			else if(_menuList.size() == 2){
				switch (_menuList[1])
				{
				case 1:
					_mDateRange = daily;
					break;
				case 2:
					_mDateRange = monthly;
					break;
				default:
					_mDateRange = dDateRange;
					break;
				}
				tempOutput << "Read loans\n";
			}
			else{
				previousPage();
				output = menuString();
			}
		}

		else if(_menuList[0] == 8){
			if(_menuList.size() == 1){
				tempOutput << "Read wish items\n";
			}
			else{
				previousPage();
				output = menuString();
			}
		}
		
		else if(_menuList[0] == 9){
			if(_menuList.size() == 1)
				tempOutput << "Undo previous action\n";
			else{
				previousPage();
				output = menuString();
			}
		}
		else if(_menuList[0] == 10){
			if(_menuList.size() == 1){
				tempOutput << "Choose a retrieve method for incomes:" << endl;
				tempOutput << "1.\tDaily" << endl;
				tempOutput << "2.\tMonthly" << endl;
				tempOutput << "3.\tUser defined Range" << endl << endl;
				tempOutput << "x. Back to Main Menu";
			}
			else if(_menuList.size() == 2){
				switch (_menuList[1])
				{
				case 1:
					_mDateRange = daily;
					break;
				case 2:
					_mDateRange = monthly;
					break;
				default:
					_mDateRange = dDateRange;
					break;
				}
				tempOutput << "Show Monthly Report\n";
			}
			else{
				previousPage();
				output = menuString();
			}
		}
		else if(_menuList[0] == 11){
			if(_menuList.size() == 1)
				tempOutput << "Import Report\n";
			else{
				previousPage();
				output = menuString();
			}
		}
		else if(_menuList[0] == 12){
			if(_menuList.size() == 1)
				tempOutput << "Edit User Profile\n";
			else{
				previousPage();
				output = menuString();
			}
		}
		else{
			previousPage();
			output = menuString();
		}
	}
		
	if(output == "")
		output=tempOutput.str();
	return output;
}

//@author A0106829Y
e_state Menu::menuState(){
	if(_menuList.size() == 0){
		return nothing;
	}
	else{
		if(_menuList[0] == -1)
			return exit_app;
		else if(_menuList[0] == 1){
			
			if(_menuList.size() == 1)
				return nothing;
			else if(_menuList.size() == 2)
				return nothing;
			else if(_menuList.size() == 3)
				return recordNewExpense;
		}

		else if(_menuList[0] == 2){
			
			if(_menuList.size() == 1)
				return recordNewIncome;
		}

		else if(_menuList[0] == 3){
			
			if(_menuList.size() == 1)
				return recordNewLoan;
		}

		else if(_menuList[0] == 4){
			
			if(_menuList.size() == 1)
				return recordNewWish;
		}

		else if(_menuList[0] == 5){
		
			if(_menuList.size() == 1){
				return nothing;
			}else if(_menuList.size() == 2)
				return readExpense;
		}

		else if(_menuList[0] == 6){
		
			if(_menuList.size() == 1){
				return nothing;
			}else if(_menuList.size() == 2)
				return readIncome;
		
		}

		else if(_menuList[0] == 7){
		
			if(_menuList.size() == 1){
				return nothing;
			}else if(_menuList.size() == 2)
				return readLoan;
		
		}

		else if(_menuList[0] == 8){
			
			if(_menuList.size() == 1)
				return readWish;
			
		}

		else if(_menuList[0] == 9){
			if(_menuList.size() == 1)
				return Undo;
			
		}

		else if(_menuList[0] == 10){
			if(_menuList.size() == 1){
				return nothing;
			}else if(_menuList.size() == 2)
				return showReport;
			
		}

		else if(_menuList[0] == 11){
			if(_menuList.size() == 1)
				return importReport;
			
		}

		else if(_menuList[0] == 12){
			if(_menuList.size() == 1)
				return changeUser;
			
		}


	}
	return nothing;
}